package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	GetGroupMemberInfoRequest struct {
		GroupID                         string   `json:"GroupId"`
		MemberInfoFilter                []string `json:"MemberInfoFilter"`
		MemberRoleFilter                []string `json:"MemberRoleFilter"`
		AppDefinedDataFilterGroupMember []string `json:"AppDefinedDataFilter_GroupMember"`
		Limit                           int      `json:"Limit"`
		Offset                          int      `json:"Offset"`
	}

	GroupMemberInfoResponse struct {
		ActionStatus string `json:"ActionStatus"`
		ErrorInfo    string `json:"ErrorInfo"`
		ErrorCode    int    `json:"ErrorCode"`
		Next         string `json:"Next"`
		MemberNum    int    `json:"MemberNum"`
		MemberList   []struct {
			MemberAccount        string `json:"Member_Account"`
			Role                 string `json:"Role"`
			JoinTime             int    `json:"JoinTime"`
			MsgSeq               int    `json:"MsgSeq"`
			MsgFlag              string `json:"MsgFlag"`
			LastSendMsgTime      int    `json:"LastSendMsgTime"`
			MuteUntil            int    `json:"MuteUntil"`
			AppMemberDefinedData []struct {
				Key   string `json:"Key"`
				Value string `json:"Value"`
			} `json:"AppMemberDefinedData"`
		} `json:"MemberList"`
	}
)

// GetGroupMemberInfo 获取群成员详细资料
func (s sdk) GetGroupMemberInfo(req GetGroupMemberInfoRequest) *GroupMemberInfoResponse {
	reqUrl := s.BuildRestfulUrl("/group_open_http_svc/get_group_member_info")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("get group member info  error: ", err)
		return nil
	}

	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		global.Log.Error("get group member info  error: ", err)
		return nil
	}
	resp := GroupMemberInfoResponse{}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("get group member info  error: ", err)
		return nil
	}
	return &resp
}
